package handlers

import (
	"net/http"
	"ssr/common"
	"ssr/dto"
	service "ssr/services"

	"github.com/gin-gonic/gin"
)

type WaterHandler struct {
	waterService *service.WaterService
}

func NewWaterHandler() *WaterHandler {
	return &WaterHandler{
		waterService: service.NewWaterService(),
	}
}

func (h *WaterHandler) CreateDict(c *gin.Context) {
	var dict dto.DictCreateDTO
	if err := c.ShouldBindJSON(&dict); err != nil {
		c.Set("already_sent", true)
		common.Fail(http.StatusBadRequest, "Invalid request parameters").Send(c)
		return
	}
	result, err := h.waterService.CreateDict(dict)
	if err != nil {
		c.Set("already_sent", true)
		common.Fail(http.StatusBadRequest, err.Error()).Send(c)
		return
	}
	common.Success(result).Send(c)
}
func (h *WaterHandler) GetDictList(c *gin.Context) {
	var queryParams dto.QueryDictListDTO
	if err := c.ShouldBindJSON(&queryParams); err != nil {
		c.Set("already_sent", true)
		common.Fail(http.StatusBadRequest, "Invalid request parameters").Send(c)
		return
	}
	result, err := h.waterService.GetDictList(queryParams)
	if err != nil {
		c.Set("already_sent", true)
		common.Fail(http.StatusBadRequest, err.Error()).Send(c)
		return
	}
	common.Success(result).Send(c)
}
func (h *WaterHandler) DeleteDict(c *gin.Context) {
	var dict dto.DeleteDictDTo
	if err := c.ShouldBindJSON(&dict); err != nil {
		c.Set("already_sent", true)
		common.Fail(http.StatusBadRequest, "Invalid request parameters").Send(c)
		return
	}
	result, err := h.waterService.DeleteDict(dict.DictId)
	if err != nil {
		c.Set("already_sent", true)
		common.Fail(http.StatusBadRequest, err.Error()).Send(c)
		return
	}
	common.Success(result).Send(c)
}
func (h *WaterHandler) Recharge(c *gin.Context) {
	var dict dto.RechargeDTO
	if err := c.ShouldBindJSON(&dict); err != nil {
		c.Set("already_sent", true)
		common.Fail(http.StatusBadRequest, "Invalid request parameters").Send(c)
		return
	}
	result, err := h.waterService.Recharge(dict.DictId, dict.Amount)
	if err != nil {
		c.Set("already_sent", true)
		common.Fail(http.StatusBadRequest, err.Error()).Send(c)
		return
	}
	common.Success(result).Send(c)
}
func (h *WaterHandler) GetWaterOrderList(c *gin.Context) {
	var queryParams dto.QueryOrderListDTO
	if err := c.ShouldBindJSON(&queryParams); err != nil {
		c.Set("already_sent", true)
		common.Fail(http.StatusBadRequest, "Invalid request parameters").Send(c)
		return
	}
	result, err := h.waterService.GetWaterOrderList(queryParams)
	if err != nil {
		c.Set("already_sent", true)
		common.Fail(http.StatusBadRequest, err.Error()).Send(c)
		return
	}
	common.Success(result).Send(c)
}

func (h *WaterHandler) CreateOrder(c *gin.Context) {
	var order dto.CreateOrderDTO
	if err := c.ShouldBindJSON(&order); err != nil {
		c.Set("already_sent", true)
		common.Fail(http.StatusBadRequest, "Invalid request parameters").Send(c)
		return
	}
	result, err := h.waterService.CreateOrder(order)
	if err != nil {
		c.Set("already_sent", true)
		common.Fail(http.StatusBadRequest, err.Error()).Send(c)
		return
	}
	common.Success(result).Send(c)
}

func (h *WaterHandler) DeleteOrder(c *gin.Context) {
	var queryParams dto.DeleteOrderDTO
	if err := c.ShouldBindJSON(&queryParams); err != nil {
		c.Set("already_sent", true)
		common.Fail(http.StatusBadRequest, "Invalid request parameters").Send(c)
		return
	}
	result, err := h.waterService.DeleteOrder(queryParams.OrderId)
	if err != nil {
		c.Set("already_sent", true)
		common.Fail(http.StatusBadRequest, err.Error()).Send(c)
		return
	}
	common.Success(result).Send(c)
}

func (h *WaterHandler) GetOrderDetail(c *gin.Context) {
	var queryParams dto.DeleteOrderDTO
	if err := c.ShouldBindJSON(&queryParams); err != nil {
		c.Set("already_sent", true)
		common.Fail(http.StatusBadRequest, "Invalid request parameters").Send(c)
		return
	}
	result, err := h.waterService.GetOrderDetail(queryParams.OrderId)
	if err != nil {
		c.Set("already_sent", true)
		common.Fail(http.StatusBadRequest, err.Error()).Send(c)
		return
	}
	common.Success(result).Send(c)
}

func (h *WaterHandler) ConfirmOrder(c *gin.Context) {
	var queryParams dto.ConfirmOrderDTO
	if err := c.ShouldBindJSON(&queryParams); err != nil {
		c.Set("already_sent", true)
		common.Fail(http.StatusBadRequest, "Invalid request parameters").Send(c)
		return
	}
	result, err := h.waterService.ConfirmOrder(queryParams.OrderId, queryParams.Status)
	if err != nil {
		c.Set("already_sent", true)
		common.Fail(http.StatusBadRequest, err.Error()).Send(c)
		return
	}
	common.Success(result).Send(c)
}
func (h *WaterHandler) CancelOrder(c *gin.Context) {
	var queryParams dto.DeleteOrderDTO
	if err := c.ShouldBindJSON(&queryParams); err != nil {
		c.Set("already_sent", true)
		common.Fail(http.StatusBadRequest, "Invalid request parameters").Send(c)
		return
	}
	result, err := h.waterService.CancelOrder(queryParams.OrderId)
	if err != nil {
		c.Set("already_sent", true)
		common.Fail(http.StatusBadRequest, err.Error()).Send(c)
		return
	}
	common.Success(result).Send(c)
}
